﻿using ICAM.Model;
using ICAM.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ICAM.ViewModel
{
    public class AlarmItemViewModule : ViewModelBase
    {
        private AlarmInfo alarmInfo;
        ///告警ID
        public String AlarmID
        {
            get { return this.alarmInfo.AlarmID; }
            set
            {
                this.alarmInfo.AlarmID = value;
            }
        }
        ///告警类型编号
        public String AlarmTypeCode
        {
            get { return this.alarmInfo.AlarmTypeCode; }
            set
            {
                this.alarmInfo.AlarmTypeCode = value;
            }
        }
        ///告警类型名称
        public String AlarmTypeName
        {
            get { return this.alarmInfo.AlarmTypeName; }
            set
            {
                this.alarmInfo.AlarmTypeName = value;
            }
        }
        ///告警时间
        public String AlarmTime
        {
            get { return this.alarmInfo.AlarmTime; }
            set
            {
                this.alarmInfo.AlarmTime = value;
            }
        }
        ///告警小区
        public String CommunityID
        {
            get { return this.alarmInfo.CommunityID; }
            set
            {
                this.alarmInfo.CommunityID = value;
            }
        }
        ///告警发生地
        public String Location
        {
            get { return this.alarmInfo.Location; }
            set
            {
                this.alarmInfo.Location = value;
            }
        }
        ///告警对象信息
        public String AlarmObjInfo
        {
            get { return this.alarmInfo.AlarmObjInfo; }
            set
            {
                this.alarmInfo.AlarmObjInfo = value;
            }
        }
        ///告警描述
        public String AlarmDesc
        {
            get { return this.alarmInfo.AlarmDesc; }
            set
            {
                this.alarmInfo.AlarmDesc = value;
                this.OnPropertyChanged("AlarmDesc");
            }
        }
        ///告警级别
        public String AlarmLevel
        {
            get { return this.alarmInfo.AlarmLevel; }
            set
            {
                this.alarmInfo.AlarmLevel = value;
            }
        }
        ///告警大图url
        public String AlarmPhotoUrl
        {
            get { return this.alarmInfo.AlarmPhotoUrl; }
            set
            {
                this.alarmInfo.AlarmPhotoUrl = value;
                this.OnPropertyChanged("AlarmPhotoUrl");
            }
        }
        ///告警最新图片url
        public String AlarmLatestPhotoUrl
        {
            get { return this.alarmInfo.AlarmLatestPhotoUrl; }
            set
            {
                this.alarmInfo.AlarmLatestPhotoUrl = value;
                this.OnPropertyChanged("AlarmLatestPhotoUrl");
            }
        }
        //---------------------告警还原小图列表-------------------
        public List<AlarmDetail.AlarmRestorationInfo> ListAlarmRestorationInfo
        {
            get { return this.alarmInfo.ListAlarmRestorationInfo; }
            set
            {
                this.alarmInfo.ListAlarmRestorationInfo = value;
                this.OnPropertyChanged("ListAlarmRestorationInfo");
            }
        }
        //---------------------告警处理----------------------------
        ///告警处理状态
        public String AlarmDealStatus
        {
            get { return this.alarmInfo.AlarmDealStatus; }
            set
            {
                this.alarmInfo.AlarmDealStatus = value;
                this.OnPropertyChanged("AlarmDealStatus");
            }
        }
        ///告警处理类型（忽略/处理）
        public String AlarmDealType
        {
            get { return this.alarmInfo.AlarmDealType; }
            set
            {
                this.alarmInfo.AlarmDealType = value;
                this.OnPropertyChanged("AlarmDealType");
            }
        }
        ///告警处理模板
        public String AlarmDealTempCode
        {
            get { return this.alarmInfo.AlarmDealTempCode; }
            set
            {
                this.alarmInfo.AlarmDealTempCode = value;
                this.OnPropertyChanged("AlarmDealTempCode");
            }
        }
        ///告警处理人
        public string AlarmDealPeople
        {
            get { return this.alarmInfo.AlarmDealPeople; }
            set
            {
                this.alarmInfo.AlarmDealPeople = value;
                this.OnPropertyChanged("AlarmDealPeople");
            }
        }
        ///告警处理时间
        public string AlarmDealTime
        {
            get { return this.alarmInfo.AlarmDealTime; }
            set
            {
                this.alarmInfo.AlarmDealTime = value;
                this.OnPropertyChanged("AlarmDealTime");
            }
        }
        ///告警处理备注
        public string AlarmDealRemake
        {
            get { return this.alarmInfo.AlarmDealRemake; }
            set
            {
                this.alarmInfo.AlarmDealRemake = value;
                this.OnPropertyChanged("AlarmDealRemake");
            }
        }

        private bool isDeal = false;
        public bool IsDeal
        {
            get { return isDeal; }
            set
            {
                isDeal = value;
                this.OnPropertyChanged("IsDeal");
            }
        }

        public ICommand CancelCommand { get; set; }
        public ICommand ConfirmCommand { get; set; }

        public ICommand ShowAlarmCommand { get; set; }

        private User user;
        public AlarmItemViewModule(AlarmInfo info, User userInfo)
        {
            this.alarmInfo = info;
            this.user = userInfo;

            this.CancelCommand = new DelegateCommand(RunCancelCommand);
            this.ConfirmCommand = new DelegateCommand(RunConfirmCommand);
            this.ShowAlarmCommand = new DelegateCommand(RunShowAlarmCommand);
 
            IsDeal = this.AlarmDealType != "未处理";
        }

        private void UpdateStatus()
        {
            this.IsDeal = this.alarmInfo.AlarmDealStatus != "0";
        }
        // 查看告警详情
        private void RunShowAlarmCommand(object param)
        {
            AlarmDetailView view = new AlarmDetailView();
            view.DataContext = this;
            view.Owner = Application.Current.MainWindow;
            view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            view.ShowDialog();
        }

        // 忽略命令
        private void RunCancelCommand(object param)
        {
            if (this.alarmInfo == null)
            {
                return;
            }
            var view = new NothingAlaramView(this);
            view.Owner = Application.Current.MainWindow;
            view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (view.ShowDialog() ?? false)
            { 
                this.AlarmDealPeople = user.LoginName;
                UpdateStatus();
            }
        }
        // 处理命令
        private void RunConfirmCommand(object param)
        {
            if (this.alarmInfo == null)
            {
                return;
            }
            var view = new DealAlaramView(this);
            view.Owner = Application.Current.MainWindow;
            view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (view.ShowDialog() ?? false)
            {
                this.AlarmDealPeople = user.LoginName;

                UpdateStatus();
            }
        }
    }
}
